<?php
/**
 *
 */
namespace Yz\modules\Modules;

use Yz_Hash;

class Model_ActionsList
    extends \Yz\modules\Generic\Model_List
{


    /** get the list of all controllers defined in the module
     *  the method relies on the assumption that controller classes are not redefined in
     *  the namespaced constants
     *  @param Yz_Module_Interface $module
     *  @return array
     */
    public function buildControllersList(\Yz_Module_Interface $module)
    {
        debug();
        $list = array();
        $i18n = $module->getI18N();
        $toScan = array($module) + class_parents($module);
        foreach ($toScan as $modClass) {
            debug("Scanning module {$modClass}");
            $ns = $modClass::namespaceName();
            if ("" === $ns) {
                break;
            }
            $path = strtr($ns, '\\', '/');
            $glob = YZ_LIB . '/' . $path . '/Controller/*.php';
            debug("Scanning path {$glob}");
            $files = glob($glob);
            if ($files) {
                foreach ($files as $file) {
                    $ctlName = basename($file, '.php');
                    debug("Scanning file {$file}, controller {$ctlName}");
                    if (! array_key_exists($ctlName, $list)) {
                        debug("Adding controller {$ctlName}");
                        include_once($file);
                        $class = $ns . '\\Controller_' . $ctlName;
                        $refl = new \ReflectionClass($class);
                        if (! $refl->isAbstract()) {
                            $list[$ctlName] = new Yz_Hash(array(
                                'inMenu' => $refl->getConstant('IN_MENU'),
                                'name' => $ctlName,
                                'title' => $i18n->translate(':' . $ctlName),
                                'className' => $class,
                                // TODO translate class name
                            ));
                        }
                    }
                }
            }
        }
        return new Yz_Hash($list);
    }


}
